////
////  AppDelegate+DTCAppService.m
////  BluetoothProject
////
////  Created by JianWei Chen on 16/9/5.
////  Copyright © 2016年 JianWei Chen. All rights reserved.
////
//
#import "AppDelegate+DTCAppService.h"
#import "WiFiModelManager.h"

//
//#import <KSCrash/KSCrashInstallationStandard.h>
////#import "GetAreaListApi.h"
//
#define NOTIFY_NET_CHANGE @"notifyNetChange"

@interface AppDelegate ()<DTCloudOperationDelegate>

@end

@implementation AppDelegate (DTCAppService)

- (void)setNetworkBaseConfig
{
    YTKNetworkConfig *config = [YTKNetworkConfig sharedConfig];
    config.baseUrl = ServerURL;
}

//- (void)setKSCrashConfig
//{
//    KSCrashInstallationStandard* installation = [KSCrashInstallationStandard sharedInstance];
//    installation.url = [NSURL URLWithString:@"https://collector.bughd.com/kscrash?key=87bb2d0ab40da4d31d444faa396ab768"];
//    [installation install];
//    [installation sendAllReportsWithCompletion:nil];
//}

- (void)setDTCloudConfig
{
    [[DTCloudManager defaultJNI_iOS_SDK]setSanBox:NO];
    [[DTCloudManager defaultJNI_iOS_SDK]setOperationDelegate:self];
    [[DTCloudManager defaultJNI_iOS_SDK]startAppId:PRODUCTID appKey:DTCloudKitAppKey successCallback:nil errorCallback:nil];
}

- (void)setWindowBaseConfig
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [[UIApplication sharedApplication]setStatusBarStyle:UIStatusBarStyleLightContent];
    
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [[IQKeyboardManager sharedManager]setEnable:YES];
    [[IQKeyboardManager sharedManager]setKeyboardDistanceFromTextField:100];
}

#pragma mark - 代理
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wobjc-protocol-method-implementation"
/**
 *  DtCloud收到设备的命令
 *
 *  @param data  设备命令
 *  @param macAddress 设备的标识
 */
- (void)dtCloudManagerMessageReceive:(NSData *)data macAddress:(NSString *)macAddress
{
    
}
/**
 *  发送到DtCloud命令失败
 *
 *  @param cmdtag 失败的命令标识
 */
- (void)dtCloudManagerMessageSendFaild:(NSUInteger)cmdtag
{
    
}
/**
 *  DtCloud发送的设备上线下线的回调
 *
 *  @param isonline 上线下线
 *  @param macAddress      设备的标识
 */
- (void)dtCloudManagerBackDeviceState:(BOOL)isonline macAddress:(NSString *)macAddress
{
    [[NSNotificationCenter defaultCenter]postNotificationName:NOTIFY_ONLINESTATUS_CHANGE object:@{@"isonline":@(isonline),@"macAddress":macAddress}];
    NSLog(@"%@%@",macAddress,isonline == YES ? @"在线":@"不在线");
}

/**
 *  DTCloud收到设备的命令
 *
 *  @param backCode    返回状态码
 *  @param backContent 返回内容
 *  @param macAddress  设备的标识
 */
- (void)dtCloudManagerMessageReceiveBackCode:(NSString *)backCode backContent:(NSString *)backContent macAddress:(NSString *)macAddress
{
    NSString *mac = [NSString toLower:[AppDelegate defaultService].currentDevice.macAddress];
    if ([mac isEqualToString:macAddress]) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"DeviceStateNotification" object:backContent userInfo:@{@"backCode":backCode,@"macAddress":macAddress}];
    }
    
//    NSLog(@"backContent:%@,backCode:%@",backContent,backCode);
//
//    NSLog(@"content:%@,backCode:%@",backContent,backCode);
//    ResponseModel *model = [WiFiModelManager initResponseWithReceiveParams:@{@"backCode":backCode,
//                                                                             @"backContent":backContent,
//                                                                             @"macAddress":macAddress}];
//    if (model != nil) {
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"DeviceStateNotification" object:model];
//    }
}

/**
 *  DTCloud用户登录凭证过期回调，需重新登录
 */
- (void)dtCloudManagerUserLoginTokenHasExpired
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"登录超时" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        DTCSAVE_OBJECT(@"NO", LOGIN);
        [[AppDelegate defaultService]setRootViewController];
        
    }];
    
    [alertController addAction:okAction];
    
    [[AppDelegate defaultService].window.rootViewController presentViewController:alertController animated:YES completion:nil];
//    [SVProgressHUD showWithStatus:@"登录超时"];
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        DTCSAVE_OBJECT(@"NO", LOGIN);
//        [[AppDelegate defaultService]setRootViewController];
//    });


}

@end
